package edu.oregonstate.vision.common;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.JFrame;

import com.sun.media.jai.widget.DisplayJAI;

public class ImageUtility {
	public static PlanarImage getImage(File file) {
		PlanarImage image = JAI.create("fileload", file.getAbsolutePath());
		return image;
	}

	public static PlanarImage convertToGrayScale(PlanarImage inputImage) {

		BufferedImage bufferedImage = new BufferedImage(inputImage.getWidth(),
				inputImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);

		Graphics graphics = bufferedImage.getGraphics();
		graphics.drawImage(inputImage.getAsBufferedImage(), 0, 0, null);
		graphics.dispose();
		return PlanarImage.wrapRenderedImage(bufferedImage);
	}
	public static void showImage(BufferedImage image) {
		JFrame frame = new JFrame();
		DisplayJAI imagePanel = new DisplayJAI();
		imagePanel.set(image);
		imagePanel.setSize(JFrame.MAXIMIZED_BOTH, JFrame.MAXIMIZED_BOTH);
		frame.add(imagePanel);
		frame.setSize(JFrame.MAXIMIZED_VERT, JFrame.MAXIMIZED_HORIZ);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

	}
	
	public static void showImage(PlanarImage image) {
		JFrame frame = new JFrame();
		DisplayJAI imagePanel = new DisplayJAI();
		imagePanel.set(image);
		imagePanel.setSize(JFrame.MAXIMIZED_BOTH, JFrame.MAXIMIZED_BOTH);
		frame.add(imagePanel);
		frame.setSize(JFrame.MAXIMIZED_VERT, JFrame.MAXIMIZED_HORIZ);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

	}
}
